package com.netease.xone.dataMgr;

import java.util.LinkedHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.text.Spannable;
import android.text.style.BackgroundColorSpan;
import android.text.style.ImageSpan;

import com.netease.xone.R;

/**
 * 表情管理器，负责表情名称、图标的访问，表情使用频率管理，以及对文本中的表情（文字）Span化。
 */
public class EmoticonMgr1 {
	

	private static EmoticonMgr1 sInstance;
	private Context mContext;

	/* 表情Icon映射表 */
	public static LinkedHashMap<String, Integer> mPager1;
	public static LinkedHashMap<String, Integer> mPager2;
	public static LinkedHashMap<String, Integer> mPager3;
	private static Pattern mEmoticonPattern = Pattern.compile("\\[(\\S+?)\\]");


	public synchronized static EmoticonMgr1 getInstance(Context context) {
		if (sInstance == null) {
			sInstance = new EmoticonMgr1(context);
		}
		return sInstance;
	}

	public static LinkedHashMap<String, Integer> getEmoticonList(int pos) {
	    switch (pos) {
            case 0:
                return mPager1;

            case 1:
                return mPager2;

            case 2:
                return mPager3;

            default:
                return null;
        }
	    
	}
	
	private EmoticonMgr1(Context context) {
		mContext = context;
	}

	/**
	 * 通过表情名称获取表情Icon图像。
	 * 
	 * @param phrase
	 * @return
	 */
	public int getIcon(String phrase) {
		Integer I = null;

		
		if (I == null || I == 0) {
			I = mPager1.get(phrase);
		}
		
		if (I == null || I == 0) {
			I = mPager2.get(phrase);
		}
		if (I == null || I == 0) {
			I = mPager3.get(phrase);
		}
	
		if (I == null) {
			return 0;
		}

		return I.intValue();
	}

	/**
     * 将一段文字中的表情（名称字符）转换为表情Span。
     * 
     * @param sp
     * @param select
     *            edit 中光标位置，select[0]:start, select[1]:end
     * @return
     */
    public int setEmoticonSpan(Spannable sp, int[] select) {
        int spanCount = 0;
        if (sp == null || sp.length() < 2)
            return spanCount;

        Matcher emoMatcher = mEmoticonPattern.matcher(sp);

        EmoticonSpan[] spans = sp.getSpans(0, sp.length(), EmoticonSpan.class);

        while (true) {
            if (!emoMatcher.find())
                break;

            int begin = emoMatcher.start();
            int end = emoMatcher.end();
            if (begin < end) {
                String phrase = sp.subSequence(begin, end).toString();
                if (isSpanExist(spans, sp, phrase, begin, end)) // 已经设置过的表情Span则不再处理。
                    continue;

                int resId = getIcon(phrase);
                if (resId > 0) {
                    /* 去掉背景Span */
                    BackgroundColorSpan[] bgcSpns = sp.getSpans(begin, end,
                            BackgroundColorSpan.class);
                    if (bgcSpns != null) {
                        for (int i = 0; i < bgcSpns.length; i++)
                            sp.removeSpan(bgcSpns[i]);
                    }

                    /* 换上表情Span */
                    sp.setSpan(new EmoticonSpan(mContext, resId,
                            ImageSpan.ALIGN_BASELINE, phrase), begin, end,
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

                    /*
                     * 之前被选中的文字如果含有表情的名称，那么现在它已经被替代为icon的span，就去掉icon上的选中效果（更改select
                     * ）
                     */
                    if (select != null) {
                        if (select[0] > begin && select[0] < end) {
                            select[0] = end;
                        }
                        if (select[1] > begin && select[1] < end) {
                            select[1] = end;
                        }
                    }
                    spanCount++;
                }
            }
        }

        return spanCount + spans.length;
    }

    /**
     * 判断指定的表情Span是否存在。
     * 
     * @param spans
     *            表情Span列表
     * @param sp
     * @param phrase
     *            表情Span名称
     * @param begin
     *            表情Span起始位置
     * @param end
     *            表情Span结束位置
     * @return
     */
    private boolean isSpanExist(EmoticonSpan[] spans, Spannable sp,
            String phrase, int begin, int end) {
        if (spans == null || spans.length == 0)
            return false;

        int count = spans.length;
        for (int i = 0; i < count; i++) {
            int spanStart = sp.getSpanStart(spans[i]);
            int spanEnd = sp.getSpanEnd(spans[i]);

            if (spanStart != begin || spanEnd != end)
                continue;

            if (spanStart == begin && spanEnd == end
                    && spans[i].mPhrase.equals(phrase))
                return true;
        }

        return false;
    }

    /** 表情Span，它是一个ImageSpan */
    private class EmoticonSpan extends ImageSpan {
        String mPhrase;

        public EmoticonSpan(Context context, int resourceId,
                int verticalAlignment, String phrase) {
            super(context, resourceId, verticalAlignment);
            mPhrase = phrase;
        }
    }
    
	static {
	    mPager1 = new LinkedHashMap<String, Integer>();
	    mPager1.put("[微笑]", R.drawable.emoticon1_t2x);
        mPager1.put("[开怀笑]", R.drawable.emoticon2_t2x);
        mPager1.put("[眨眼]", R.drawable.emoticon3_t2x);
        mPager1.put("[惊讶]", R.drawable.emoticon4_t2x);
        mPager1.put("[吐舌笑脸]", R.drawable.emoticon5_t2x);
        mPager1.put("[得意的笑]", R.drawable.emoticon6_t2x);
        mPager1.put("[生气]", R.drawable.emoticon7_t2x);
        mPager1.put("[怕怕]", R.drawable.emoticon8_t2x);
        mPager1.put("[尴尬]", R.drawable.emoticon9_t2x);
        mPager1.put("[难过]", R.drawable.emoticon10_t2x);
        mPager1.put("[哭泣]", R.drawable.emoticon11_t2x);
        mPager1.put("[失望]", R.drawable.emoticon12_t2x);
        mPager1.put("[困了]", R.drawable.emoticon13_t2x);
        mPager1.put("[好好笑]", R.drawable.emoticon14_t2x);
        mPager1.put("[啵]", R.drawable.emoticon15_t2x);
        mPager1.put("[电到了]", R.drawable.emoticon16_t2x);
        mPager1.put("[汗]", R.drawable.emoticon17_t2x);
        mPager1.put("[流口水]", R.drawable.emoticon18_t2x);
        mPager1.put("[真困啊]", R.drawable.emoticon19_t2x);
        mPager1.put("[我吐]", R.drawable.emoticon20_t2x);
        mPager1.put("[问号]", R.drawable.emoticon21_t2x);
        mPager1.put("[嘘]", R.drawable.emoticon22_t2x);
        mPager1.put("[砸死你]", R.drawable.emoticon23_t2x);
        mPager1.put("[不说]", R.drawable.emoticon24_t2x);
        mPager1.put("[坏]", R.drawable.emoticon25_t2x);
        mPager1.put("[色迷迷]", R.drawable.emoticon26_t2x);
        mPager1.put("[教训]", R.drawable.emoticon27_t2x);
        mPager1.put("[可爱]", R.drawable.emoticon28_t2x);
        mPager1.put("[耶]", R.drawable.emoticon29_t2x);
        mPager1.put("[崩溃]", R.drawable.emoticon30_t2x);
        mPager1.put("[鄙视你]", R.drawable.emoticon31_t2x);
        mPager1.put("[删除]", R.drawable.btn_keyboard_del_selector);
        
        mPager2 = new LinkedHashMap<String, Integer>();
        mPager2.put("[开心]", R.drawable.emoticon32_t2x);
        mPager2.put("[仰慕你]", R.drawable.emoticon33_t2x);
        mPager2.put("[晕]", R.drawable.emoticon34_t2x);
        mPager2.put("[挖鼻孔]", R.drawable.emoticon35_t2x);
        mPager2.put("[撒娇]", R.drawable.emoticon36_t2x);
        mPager2.put("[鼓掌]", R.drawable.emoticon37_t2x);
        mPager2.put("[害羞]", R.drawable.emoticon38_t2x);
        mPager2.put("[老大]", R.drawable.emoticon39_t2x);
        mPager2.put("[欠揍]", R.drawable.emoticon40_t2x);
        mPager2.put("[飞吻]", R.drawable.emoticon41_t2x);
        mPager2.put("[工作忙]", R.drawable.emoticon42_t2x);
        mPager2.put("[大哭]", R.drawable.emoticon43_t2x);
        mPager2.put("[偷偷笑]", R.drawable.emoticon44_t2x);
        mPager2.put("[送花给你]", R.drawable.emoticon45_t2x);
        mPager2.put("[来亲一个]", R.drawable.emoticon46_t2x);
        mPager2.put("[拍桌子]", R.drawable.emoticon47_t2x);
        mPager2.put("[拜拜]", R.drawable.emoticon48_t2x);
        mPager2.put("[玫瑰]", R.drawable.emoticon49_t2x);
        mPager2.put("[好爱你]", R.drawable.emoticon50_t2x);
        mPager2.put("[心碎了]", R.drawable.emoticon51_t2x);
        mPager2.put("[好]", R.drawable.emoticon52_t2x);
        mPager2.put("[不好]", R.drawable.emoticon53_t2x);
        mPager2.put("[握个手]", R.drawable.emoticon54_t2x);
        mPager2.put("[睫毛弯弯]", R.drawable.emoticon55_t2x);
        mPager2.put("[嗷嗷嗷]", R.drawable.emoticon56_t2x);
        mPager2.put("[秀一下]", R.drawable.emoticon57_t2x);
        mPager2.put("[扭捏]", R.drawable.emoticon58_t2x);
        mPager2.put("[阿弥陀佛]", R.drawable.emoticon59_t2x);
        mPager2.put("[摸摸]", R.drawable.emoticon60_t2x);
        mPager2.put("[抓狂]", R.drawable.emoticon61_t2x);
        mPager2.put("[啾啾]", R.drawable.emoticon62_t2x);
        mPager2.put("[删除]", R.drawable.btn_keyboard_del_selector);
        
        mPager3 = new LinkedHashMap<String, Integer>();
        mPager3.put("[耍酷]", R.drawable.emoticon63_t2x);
        mPager3.put("[叹气]", R.drawable.emoticon64_t2x);
        mPager3.put("[飘啊飘]", R.drawable.emoticon65_t2x);
        mPager3.put("[气呼呼]", R.drawable.emoticon66_t2x);
        mPager3.put("[很得意]", R.drawable.emoticon67_t2x);
        mPager3.put("[无辜]", R.drawable.emoticon68_t2x);
        mPager3.put("[亲亲]", R.drawable.emoticon69_t2x);
        mPager3.put("[删除]", R.drawable.btn_keyboard_del_selector);
	}
}
